<?php
/**
 * 搜索处理类
 * @author Ice <iceinto@mallog.com.cn>
 */
class SearchAction extends BaseAction
{
    public function index()
    {
        $name = trim($this->_get('keyword', ''));
        if (empty($name)) {
            $this->error('搜索关键字不存在！');
        } else {
            $name = htmlspecialchars_decode($name);
        }
        $BrandModel = D('Brand');
        $brand = $BrandModel->getOneByEname($name);
        //TODO 添加缓存处理
        if (!empty($brand)) {
            //品牌详细信息 获取图片
            $image = $BrandModel->getByIdDetailAndImage($brand['id']);
            $brand['image_url'] = $image['image_url'];
            //获取关注
            $brand['att_count'] = D('BrandAttention')->getAttCount($brand['id']);
            $BrandModel->attCheckCount($brand['id'], $brand['att_count']);
            //获取展示图
            $brand['slide'] = D('Slide')->getBrandOne($brand['id']);
            //获取品牌柜台
            $FloorBrandModel = D('FloorBrand');
            $BrandNewsModel = D('BrandNews');
            $map = array();
            $map['m_brand.id'] = $brand['id'];
            $floorBrands = $FloorBrandModel->getSearchBrand($map);
            foreach ($floorBrands as $key => $b) {
                $floorBrands[$key]['dis'] = $BrandNewsModel->getOneNew($b['mid'], $b['fid'], $b['bid']);
            }
            //排序
            $fdis = array();
            $fnodis = array();
            foreach ($floorBrands as $key => $b) {
                if (empty($b['dis'])) {
                    $fdis[] = $b;
                } else {
                    $fnodis[] = $b;
                }
            }
            unset($floorBrands);
            //排序后进行合并数组
            $floorBrands = array_merge($fnodis, $fdis);
            $uid = getLoginUserInfo();
            if ($uid) {
                D('BrandAttention')->checkUserIsAttrBrand($uid, $brand);
            }
            $seoName = $brand['name'] . ' - ' . $brand['ename'];
            $this->seoSet($seoName, $brand['name'] . ',' . $brand['ename'], msubstr(hh($brand['intr']), 0, 200));
            $this->assign('brand', $brand);
            $this->assign('fb', $floorBrands);
        } else {
            $this->errorLang('_SEARCH_BRAND_NO_');
        }
        $this->display();
    }

    /**
     * 品牌搜索自动完成搜索
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBrands()
    {
        $data = array();
        $keyword = trim($this->_get('keyword'));
        if ($keyword) {
            $keyword = addslashes($keyword);
            if (empty($keyword)) {
            } else {
                $cacheName = 'search-keyword-' . $keyword;
                $rs = $this->getCache($cacheName);
                if (empty($rs)) {
                    $map = array();
                    $map['ename'] = array('LIKE', '%' . strtolower($keyword) . '%');
                    $map['name'] = array('LIKE', '%' . $keyword . '%');
                    $map['_logic'] = 'OR';
                    $datas = M('brand')->field('name, ename')
                        ->where($map)
                        ->order('ename')
                        ->limit('0,10')
                        ->select();
                    $ss = array();
                    foreach ($datas as $brand) {
                        unset($s);
                        $s[] = msubstr($brand['ename'] . '::' . $brand['name'], 0, 40);
                        $ss[] = $s;
                    }
                    $rs = json_encode($ss);
                    $this->setCache($cacheName, $rs);
                }
                echo $rs;
            }
        }
        exit;
    }
}

?>